package com.bbtShop.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbtShop.core.model.UserInfoDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

import static com.bbtShop.core.utils.RedisUtil.fex;

/**
 * @author : manke
 * @Description
 * @Date: 2020/11/1 11:52
 */
@Component
public class UserInfoUtil {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RedisUtil redisUtil;

    public  UserInfoDto getUserInfo(){
        String token = request.getHeader("token");
        if (token != null && !"".equals(token)){
            String userInfo = redisUtil.get(fex + token);
            JSONObject jsonObject = JSON.parseObject(userInfo);
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.convertValue(jsonObject, UserInfoDto.class);
        }
        return null;

    }
}
